package org.agilelang.main;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import org.agilelang.config.FileStatic;
import org.agilelang.generate.AgileGenerator;
import org.agilelang.generate.UtilFile;
import org.agilelang.config.AgileConfig;
import org.agilelang.config.AgileStructure;
import org.agilelang.config.AgileSyntax;
import org.agilelang.config.ReadConfig;
import org.agilelang.scanner.Scanner;
import org.agilelang.config.ScannerConfig;

public class MainTest {

    public static void main(String args[]) {
        String path = "E:\\agiletest\\PHP 3 - Controller Form";

        String folder_tmp = "tmp";
        String folder_static = "static";
        String path_config = "app.config";
        String path_source = "source.agile";

        File tmp = new File(path + File.separator + folder_tmp);
        if (!tmp.exists()) {
            if (tmp.mkdir()) {
                System.out.println("Directorio temporal creado : " + path + File.separator + folder_tmp);
            } else {
                System.err.println("Error no se puede crear directorio temporal");
            }
        } else {
            System.out.println("Directorio temporal ya existe : " + path + File.separator + folder_tmp);
        }

        tmp = new File(path + File.separator + folder_static);
        if (!tmp.exists()) {
            if (tmp.mkdir()) {
                System.out.println("Directorio statico creado : " + path + File.separator + folder_static);
            } else {
                System.err.println("Error no se puede crear directorio statico");
            }
        } else {
            System.out.println("Directorio statico ya existe : " + path + File.separator + folder_static);
        }

        ScannerConfig scanner_config = new ScannerConfig(path + File.separator + path_config, path + File.separator + folder_tmp + File.separator + "config_out.xml");
        ReadConfig read_config = new ReadConfig();
        read_config.process(path + File.separator + folder_tmp + File.separator + "config_out.xml");
        AgileConfig aconfig = read_config.getAconfig();
        AgileStructure astructure = read_config.getAstructure();
        HashMap<String, AgileSyntax> hashAgileSyntax = read_config.getHashAgileSyntax();
        //----------------------------------------------------------------------
        List<String> folders = astructure.folder;
        for (String folder : folders) {
            File file_folder = new File(aconfig.folderOut + File.separator + folder);
            if (!file_folder.exists()) {
                if (file_folder.mkdir()) {
                    System.out.println("Directorio creado : " + file_folder.getAbsolutePath());
                } else {
                    System.err.println("Error no se puede crear directorio : " + file_folder.getAbsolutePath());
                }
            } else {
                System.out.println("Directorio ya existe : " + file_folder.getAbsolutePath());
            }
        }
        //----------------------------------------------------------------------
        List<FileStatic> files = astructure.file;
        for (FileStatic archive : files) {
            File file_archive = new File(path + File.separator + folder_static + File.separator + archive.input);
            if (file_archive.exists()) {
                String content = UtilFile.readFile(path + File.separator + folder_static + File.separator + archive.input);
                UtilFile.writefile(aconfig.folderOut + File.separator + archive.output, content);
                System.out.println("Copiar archivo : " + aconfig.folderOut + File.separator + archive.output);
            } else {
                System.err.println("No se encuentra archivo  : " + path + File.separator + folder_static + File.separator + archive.input);
            }
        }
        //----------------------------------------------------------------------
        Scanner scanner = new Scanner(path + File.separator + path_source, read_config.getListObjectClass(), path + File.separator + folder_tmp + File.separator + "scanner_out.xml");
        //----------------------------------------------------------------------
        /*AgileGenerator ag = new AgileGenerator();
        ag.process( path + File.separator + folder_tmp + File.separator + "scanner_out.xml",  
                path + File.separator+aconfig.folderTemplate,  aconfig.folderOut,  read_config.getListObjectClass()) ;
        */
    }
}
